<?php
/**
 * 海外仓-中转库存（入库）
 * (model)
 */
namespace App\Models\OverSeasBa;

use App\Models\Basics\AmazonAccount;
use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlatAccount;
use App\Models\OverSeasIn\DeliveryBox;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductClassify;
use Illuminate\Database\Eloquent\Model;
use App\Models\Product\ProductSku;
class InventoryBox extends Model
{
    protected $table = 'os_b_box_inventory';

    protected $guarded = [];

    public $timestamps = false;

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function warehouse(){
        return $this->belongsTo(Warehouse::class,'warehouse_id');
    }

    public function delivery_box(){
        return $this->belongsTo(DeliveryBox::class,'delivery_box_id','id');
    }

    public function sku(){
        return $this->belongsTo(ProductSku::class,'psku_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    public static function initRelatedData($accountId,$pSkuId,$fnsku){
        $countryIds = SysPlatAccount::getCountriesById($accountId);
        return ProductAmazonSku::query()->select('account_id','fnsku')
            ->whereIn('country_id',$countryIds)
            ->where('psku_id',$pSkuId)
//            ->where('fnsku','<>',$fnsku)
            ->groupBy(['account_id','fnsku'])
            ->get();
    }
}
